//
//  AppDelegate+Alipay.m
//  BaseProject
//
//  Created by 王盼盼 on 2024/9/11.
//  Copyright © 2024 wangpanpan. All rights reserved.
//

#import "AppDelegate+Alipay.h"
#import <AlipaySDK/AlipaySDK.h>
@implementation AppDelegate (Alipay)
- (BOOL)isAlipayHost:(NSURL *)url {
    return [url.host isEqualToString:@"safepay"];
}
- (BOOL)application_alipay:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
    [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
        NSLog(@"result = %@",resultDic);
        NSString *isPaySuccess = @"0";
        NSString *msg = [resultDic objectForKey:@"memo"];;
        switch ([[resultDic objectForKey:@"resultStatus"] integerValue]) {
            case 6001: 
            {
                isPaySuccess = @"0";
                msg = @"用户中途取消";
            } 
                break;
            case 6002: 
            {
                isPaySuccess = @"0";
                msg = @"网络连接出错";
            } 
                break;
            case 6004: 
            {
                isPaySuccess = @"0";
                msg = @"支付结果未知（有可能已经支付成功），请查询商户订单列表中订单的支付状态";
            } 
                break;
            case 4000: 
            {
                isPaySuccess = @"0";
                msg = @"支付失败";
            } 
                break;
            case 8000: 
            {
                isPaySuccess = @"0";
                msg = @"正在处理中，支付结果未知（有可能已经支付成功），请查询商户订单列表中订单的支付状态";
            } 
                break;
            case 9000: 
            {
                isPaySuccess = @"1";
                msg = @"支付成功";
            } 
                break;
            default:
                break;
        }
        NSMutableDictionary *payDic = @{}.mutableCopy;
        [payDic setValue:msg forKey:@"memo"];
        [payDic setValue:msg forKey:@"msg"];
        [payDic setValue:@"alipay" forKey:@"payType"];
        [payDic setValue:isPaySuccess forKey:@"resultStatus"];
        [payDic setValue:[resultDic objectForKey:@"resultStatus"] forKey:@"code"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"payResultNotification" object:nil userInfo:payDic];
    }];
    [[AlipaySDK defaultService] processAuth_V2Result:url standbyCallback:nil];
    return YES;
}
@end
